package com.yanqu.road.logic.activity.silkroad.refloat;

import com.yanqu.road.entity.activity.silkroad.config.SilkRoadSeaConfig;
import com.yanqu.road.logic.activity.silkroad.SilkRoadConfig;
import com.yanqu.road.pb.activity.SilkRoadProto;

import java.util.ArrayList;
import java.util.List;

//海域场景数据,有复数个SilkRoadSeaItem
public class SilkRoadSeaScence {
    private int seaType;
    private long startTime;  //开始时间毫秒
    private List<SilkRoadSeaItem> silkRoadSeaItems;
    private SilkRoadHook silkRoadHook;
    public final static int maxWidth = 750;
    public double maxHeight = 200;

    public double getMaxHeight() {
        return maxHeight;
    }

    public void setMaxHeight(double maxHeight) {
        this.maxHeight = maxHeight;
    }

    public SilkRoadSeaScence(int seaType, List<SilkRoadSeaItem> silkRoadSeaItems, SilkRoadHook silkRoadHook) {
        this.silkRoadSeaItems = silkRoadSeaItems;
        this.silkRoadHook = silkRoadHook;
        this.seaType = seaType;
        this.startTime = System.currentTimeMillis();
    }

    public SilkRoadSeaScence(SilkRoadProto.SilkRoadRefloatScenceTemp scenceTempMsg, SilkRoadConfig config) {
        this.seaType = scenceTempMsg.getSeaId();
        this.silkRoadSeaItems = new ArrayList<>();
        for (SilkRoadProto.SilkRoadSeaItemTemp itemTempMsg : scenceTempMsg.getItemsList()) {
            SilkRoadSeaConfig seaItemConfig = config.getSeaItemConfig(itemTempMsg.getItemId());
            if (seaItemConfig == null) {
                continue;
            }
            SilkRoadSeaItem silkRoadSeaItem = new SilkRoadSeaItem(seaItemConfig);
            silkRoadSeaItem.setX(itemTempMsg.getX());
            silkRoadSeaItem.setY(itemTempMsg.getY());
            silkRoadSeaItem.setDirection(itemTempMsg.getDirection());
            this.silkRoadSeaItems.add(silkRoadSeaItem);
        }

        this.silkRoadHook = new SilkRoadHook(config.getHookParams());
        this.silkRoadHook.setAngle(scenceTempMsg.getHook().getAngle());
        this.silkRoadHook.setX(scenceTempMsg.getHook().getX());
        this.silkRoadHook.setY(scenceTempMsg.getHook().getY());
        this.silkRoadHook.initMove(scenceTempMsg.getHook().getInitMove());
        this.setMaxHeight(this.silkRoadHook.getY());
    }

    public void move() {
        long currentTime = System.currentTimeMillis();
        long time = currentTime - startTime;
        for (SilkRoadSeaItem silkRoadSeaItem : silkRoadSeaItems) {
            silkRoadSeaItem.move();
        }
        this.silkRoadHook.changeAngle();
    }

    public void moveByCurrentTime(long currentTimeMillis) {
        long time = currentTimeMillis - startTime;
        for (SilkRoadSeaItem silkRoadSeaItem : silkRoadSeaItems) {
            silkRoadSeaItem.moveByTime(time);
        }
        this.silkRoadHook.changeAngleByTime(time);
    }

    //hook发射后角度不再改变,判断是否碰撞到了某个物体
    public SilkRoadSeaItem launchMove() {
//        System.out.println("hook x:" + silkRoadHook.getX() + " y:" + silkRoadHook.getY() + " angle:" + silkRoadHook.getAngle());
        for (int i = 0; i < 1000; i++) {//不用while防止死循环
            if (silkRoadHook.getX() >= maxWidth ) {
                return new SilkRoadSeaItem(silkRoadHook.getReturnX(), silkRoadHook.getReturnY());
            }

            if (silkRoadHook.getX() < 0) {
                return new SilkRoadSeaItem(silkRoadHook.getReturnX(), silkRoadHook.getReturnY());
            }

            if (silkRoadHook.getY() > maxHeight) {
                return new SilkRoadSeaItem(silkRoadHook.getReturnX(), silkRoadHook.getReturnY());
            }

            if (silkRoadHook.getY() < 0) {
                return new SilkRoadSeaItem(silkRoadHook.getReturnX(), silkRoadHook.getReturnY());
            }

            silkRoadHook.move();
//            System.out.println("hook x:" + silkRoadHook.getX() + " y:" + silkRoadHook.getY() + " angle:" + silkRoadHook.getAngle());

            for (SilkRoadSeaItem silkRoadSeaItem : silkRoadSeaItems) {
                silkRoadSeaItem.move();
            }

            for (int j = 0; j < silkRoadSeaItems.size(); j++) {
                SilkRoadSeaItem silkRoadSeaItem = silkRoadSeaItems.get(j);
                if (silkRoadSeaItem.isHit(silkRoadHook)) {
                    silkRoadSeaItems.remove(j);
//                    System.out.println("hit item id:" + silkRoadSeaItem.getItemId() + " x:" + silkRoadSeaItem.getX() + " y:" + silkRoadSeaItem.getY() + " direction:" + silkRoadSeaItem.getDirection());
                    return silkRoadSeaItem;
                }
            }
        }


        return null;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public int getSeaType() {
        return seaType;
    }

    public List<SilkRoadSeaItem> getSilkRoadSeaItems() {
        return silkRoadSeaItems;
    }

    public SilkRoadHook getSilkRoadHook() {
        return silkRoadHook;
    }

    public static void main(String[] args) {

        List<SilkRoadSeaItem> silkRoadSeaItems = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            SilkRoadSeaItem silkRoadSeaItem = new SilkRoadSeaItem(i + 1, 50 * (i + 1), 50 * (i + 1), 100, 50, 10, i % 3, 200);
//            silkRoadSeaItems.add(silkRoadSeaItem);
//        }
        SilkRoadSeaItem silkRoadSeaItem = new SilkRoadSeaItem(1, 350, 50, 100, 50, 50, 1, 200);
        silkRoadSeaItems.add(silkRoadSeaItem);
        SilkRoadHook silkRoadHook = new SilkRoadHook(30, 10, 10, 60);
        SilkRoadSeaScence silkRoadSeaScence = new SilkRoadSeaScence(1, silkRoadSeaItems, silkRoadHook);
        silkRoadSeaScence.setStartTime(System.currentTimeMillis());
        silkRoadSeaScence.moveByCurrentTime(System.currentTimeMillis() + 6000);
        silkRoadSeaScence.launchMove();
    }


}
